package com.itnoob.service;

import com.itnoob.event.OrderStatusChangeEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;

/**
 * @author zhoujun
 * @date 2025/3/20 20:42
 */
@Service
public class StockService implements ApplicationListener<OrderStatusChangeEvent> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    public void onApplicationEvent(OrderStatusChangeEvent event) {
        //根据不同的订单状态,执行不同的库存操作
        switch (event.getOrderStatus()){
            case PAID:
                //锁定库存
                lockStock(event.getOrderId());
                break;
            case DELIVERED:
                //扣减库存
                deductStock(event.getOrderId());
                break;

            case RECEIVED:
                //释放库存
                releaseStock(event.getOrderId());
                break;
            default:
                break;

        }
    }


    private void lockStock(String orderId) {
        logger.info("[lockStock]锁定订单({})的库存",orderId);
    }

    private void deductStock(String orderId) {
        logger.info("[deductStock]扣减订单({})的库存",orderId);
    }

    private void releaseStock(String orderId) {
        logger.info("[releaseStock]释放订单({})的库存",orderId);
    }
}
